/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.edit.policy;

import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.ComponentEditPolicy;
import org.eclipse.gef.requests.GroupRequest;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.actions.delete.DeleteFromDiagramAction;
import com.cea.papyrus.core.actions.delete.DeleteFromModelAction;
import com.cea.papyrus.core.commands.factories.DiagramElementDeleteCommandFactory;
import com.cea.papyrus.core.commands.factories.UMLDi2ElementDeleteFactory;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.core.utils.LookForElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * This edit part is installed on all graphical elements (everything except for links) and
 * enable them to be deleted.
 * 
 * @author Andreas Limyr
 * @author frankd@ifi.uio.no
 */
public class DeletionEditPolicy extends ComponentEditPolicy {

    @Override
    protected Command createDeleteCommand(GroupRequest request) {
        return null;
    }
    
    /**
     * @see org.eclipse.gef.editpolicies.ComponentEditPolicy#getCommand(org.eclipse.gef.Request)
     */
    @Override
    public Command getCommand(Request request) {

        if (DeleteFromDiagramAction.DELETE_FROM_DIAGRAM_ACTION_REQUEST.equals(request.getType())
        		|| REQ_DELETE.equals(request.getType())) {
            return getDeleteFromDiagramCommand();
        } else if (DeleteFromModelAction.DELETE_FROM_MODEL_ACTION_REQUEST.equals(request.getType())) {
            return getDeleteFromModelCommand(request);
        }
        return super.getCommand(request);
    }
    
    private Command getDeleteFromDiagramCommand() {
    	  
    	GraphElement graphElement = (GraphElement) getHost().getModel();
    	Command deletionCommand =
    		DiagramElementDeleteCommandFactory.eINSTANCE.createCommand(graphElement);

        return deletionCommand;
    }

    private Command getDeleteFromModelCommand(Request request) {
    	GraphElement graphElement = (GraphElement) getHost().getModel();
    	Element eToDelete = null;
    	try {
    		eToDelete = LookForElement.lookForUMLElementOf(graphElement);
            return UMLDi2ElementDeleteFactory.eINSTANCE.createCommand (eToDelete);
    	} catch (Exception e) {
    		Debug.log(e);
    	}
    	
    	return null;
    }

}
